package summer.student.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import summer.student.Dao.AdminDao;
import summer.student.Dao.StudentDao;
import summer.student.Dao.TeacherDao;
import summer.student.Domain.Admin;
import summer.student.Domain.Student;
import summer.student.Domain.Teacher;
import summer.student.Utils.Judge;
import summer.student.Utils.JwtUtil;
import summer.student.Utils.Result;

import java.util.List;
import java.util.Map;

/**
 * @program: student
 * @description:
 * @author: hlt
 * @create: 2023-07-09 19:48
 **/
@RestController
@RequestMapping("/admins")
@CrossOrigin(origins = "*", maxAge = 3600)  // 后端解决跨域
@Api(tags = "超级管理员相关接口")
public class AdminController {
    @Autowired
    private AdminDao adminDao;

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private TeacherDao teacherDao;


    @GetMapping("/selectSelf")
    public Result selectSelf(@RequestParam(name = "adminId", required = true) String adminId){
        System.out.println(adminId);
        LambdaQueryWrapper<Admin> alqw = new LambdaQueryWrapper<>();
        alqw.eq(Admin::getAdminId, adminId);
        Admin admin = adminDao.selectOne(alqw);
        if (admin == null) return Result.Error("管理员信息未找到！");
        else return Result.Success(admin);
    }

    @PostMapping("/addStudent")
    public Result addStudent(@RequestBody Map<String,String>data){
        String stuId = data.get("stuId");
        String stuName = data.get("stuName");
        int stuGender = Integer.parseInt(data.get("stuGender"));
        String stuGrade = data.get("stuGrade");
        String stuPhone = data.get("stuPhone");
        if(stuId == null || stuName == null || stuGrade == null || stuPhone == null) return Result.Error("信息不全！");
        LambdaQueryWrapper<Student> slqw = new LambdaQueryWrapper<>();
        List<Student> students = studentDao.selectList(slqw);
        for(Student s : students){
            if(s.getStuId().equals(stuId)) return Result.Error("学生学号重复！");
        }
        Student student = new Student(stuId, stuName, "123456", stuGender, stuGrade, stuPhone);
        studentDao.insert(student);
        return Result.Success("学生信息导入成功！");
    }

    @PostMapping("/addTeacher")
    public Result addTeacher(@RequestBody Map<String, String>data){
        String teacherId = data.get("teacherId");
        String teacherName = data.get("teacherName");
        int teacherGender = Integer.parseInt(data.get("teacherGender"));
        String teacherPhone = data.get("teacherPhone");
        long college = Long.parseLong(data.get("college"));
        if(teacherId == "" || teacherPhone == "" || teacherName == "") return Result.Error("信息不全！");
        Teacher ret = teacherDao.selectById(teacherId);
        if(ret != null) return Result.Error("教师编号重复！");
        Teacher teacher = new Teacher(teacherId, teacherName, "123456", teacherGender, teacherPhone,college);
        teacherDao.insert(teacher);
        return  Result.Success("教师信息导入成功！");
    }

    @PostMapping("/addAdmin")
    public Result addAdmin(@RequestBody Map<String, String>data){
        String adminId = data.get("adminId");
        String adminName = data.get("adminName");
        String adminPhone = data.get("adminPhone");
        if(adminId == null || adminName == null || adminPhone == null) return Result.Error("信息不全！");
        Admin ret = adminDao.selectById(adminId);
        if (ret!=null) return Result.Error("管理员编号重复！");
        Admin admin = new Admin(adminId, adminName, adminPhone, "123456");
        adminDao.insert(admin);
        return Result.Success("管理员信息导入成功！");
    }
}
